package com.zookeeper.zk;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.List;

/**
 * @author WangGuoLiang
 * @program ZooKeeper
 * @description [用一句话描述此类]
 * @create 2021-09-04 18:42
 **/
public class zkClient {
    ZooKeeper zooKeeper;
    @Before
    public void init() throws IOException, KeeperException, InterruptedException {
         zooKeeper = new ZooKeeper("47.106.199.16:2181", 2000, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                List<String> children = null;
                try {
                    children = zooKeeper.getChildren("/", true);
                    for (String child : children) {
                        System.out.println(child);
                    }
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        });



    }
    @Test
    public void create() throws KeeperException, InterruptedException {
        String s = zooKeeper.create("/atguigus", "ssttt.avi".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);


    }
    @Test
    public void getChildren() throws KeeperException, InterruptedException {

        Thread.sleep(Integer.MAX_VALUE);
    }
    @Test
    public void exist() throws KeeperException, InterruptedException {
        Stat exists = zooKeeper.exists("/atguigus2", false);
        System.out.println(exists);
    }
}

